package com.example.sunlight.interneteverything.View;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.sunlight.interneteverything.R;

/**
 * Created by SunLight on 2017/8/17.
 */

public class TraffcRoteView extends View {
    Bitmap bitmap;
    Resources resources;
    int with, height;
    Paint paint;
    TypedArray typedArray;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    TraffcRoteView.this.invalidate();
                    break;
            }
        }
    };

    public TraffcRoteView(Context context) {
        super(context);
        init();
    }

    public void init() {
        resources = getResources();
        paint = new Paint();
        bitmap = ((BitmapDrawable) resources.getDrawable(R.drawable.weather)).getBitmap();
        with = bitmap.getWidth();
        height = bitmap.getHeight();
    }

    String type, number, unit;
    float type_left, type_top;
    float number_left, number_top;
    float num_size, type_size;


    public void setNumber(String number) {
        this.number = number;
        handler.sendEmptyMessage(0x123);
    }

    public void init(AttributeSet attrs) {
        typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TraffcRoteView);
        unit = typedArray.getString(R.styleable.TraffcRoteView_unit);
        type = typedArray.getString(R.styleable.TraffcRoteView_typet);
        number = typedArray.getString(R.styleable.TraffcRoteView_numbert);
        type_left = typedArray.getDimension(R.styleable.TraffcRoteView_type_leftt, 0);
        type_top = typedArray.getDimension(R.styleable.TraffcRoteView_type_topt, 0);
        type_size = typedArray.getDimension(R.styleable.TraffcRoteView_type_sizet, 0);

        number_left = typedArray.getDimension(R.styleable.TraffcRoteView_number_leftt, 0);
        number_top = typedArray.getDimension(R.styleable.TraffcRoteView_number_topt, 0);
        num_size = typedArray.getDimension(R.styleable.TraffcRoteView_number_sizet, 0);
    }

    public TraffcRoteView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        init(attrs);
    }

    public TraffcRoteView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        init(attrs);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(with, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawRotateBitmap(canvas, null, bitmap, 0, 0, 0);
        drawText(canvas, paint, type, type_size, type_left, type_top);
        drawText(canvas, paint, number, num_size, number_left, number_top);
        drawText(canvas, paint, unit, num_size - 15, number_left + 60, number_top);
    }

    private void drawText(Canvas canvas, Paint paint, String text, float size, float posX, float posY) {
        paint.setTextSize(size);
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        canvas.drawText(text, posX, posY, paint);
    }

    private void drawRotateBitmap(Canvas canvas, Paint paint, Bitmap bitmap, float rotation, float posX, float posY) {
        Matrix matrix = new Matrix();
        int offsetX = bitmap.getWidth() / 2;
        int offsetY = bitmap.getHeight() / 2;
        matrix.postTranslate(-offsetX, -offsetY);
        matrix.postRotate(rotation);
        matrix.postScale(0.6f, 0.6f);
        matrix.postTranslate(posX + offsetX, posY + offsetY);
        canvas.drawBitmap(bitmap, matrix, paint);
    }
}
